package b3.widget.store
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	import b3.widget.home.bottom.*;
	import b3.widget.home.town.*;
	import b3.widget.tab.*;
	import b3.widget.vip.*;
	
	import com.greensock.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.text.*;
	
	public class Store extends TabController
	{
		private static var instance:Store;
		private const titleInfo:Array = ["水立方、泰姬陵、帝国大厦，世界著名建筑任你挑选！", "天使卡、恶魔卡、原子弹、毒气，助你称霸世界！", 
			"光明区的物品用<font color='#FF0000' size='18'> 光明币 </font>进行兑换！光明币主要通过<font color='#FF0000' size='18'> 协助好友 </font>获得！", 
			"黑暗区的物品用<font color='#FF0000' size='18'> 黑暗币 </font>进行兑换！黑暗币主要通过<font color='#FF0000' size='18'> 攻击好友 </font>获得！" ];
		
		public function Store()
		{
			//所有物体
			var storeIndexes:Array = new Array();
			var objectConfig:ObjConfig;
			for (var indexStr:String in Config.objConfigs)
			{
				objectConfig = Config.objConfigs[indexStr];
				if (objectConfig.storeTabIndex >= 0)
				{
					storeIndexes.push(objectConfig.index);
				}
			}
		    storeIndexes.sort(sortObjects);
		    //设置今日特价
		   	setSpecialOfToday(storeIndexes);
		   	//初始化窗体
			var StoreBgr:Class = ResourceHelper.getInstance().getClassByName("StoreBgr");
			var TabTitleBackground:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr2");
			var HighlightTitleBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr3");
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton4");
			var TabPageFirstPage:Class = ResourceHelper.getInstance().getClassByName("TabPageFirstPage3");
			var TabPagePageUp:Class = ResourceHelper.getInstance().getClassByName("TabPagePageUp3");
			var TabPagePageDown:Class = ResourceHelper.getInstance().getClassByName("TabPagePageDown3");
			var TabPageLastPage:Class = ResourceHelper.getInstance().getClassByName("TabPageLastPage3");
			super(StoreBgr, CloseButton);
			var specialZone:SpecialZone = new SpecialZone();
			this.addTab(TabTitleBackground, specialZone.tabName, specialZone, HighlightTitleBgr);
			var tab:Tab = new Tab("许可证", 3, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(265, 248);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new Tab("道具区", 3, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(265, 248);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new Tab("光明区", 3, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(265, 248);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new Tab("黑暗区", 3, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0x66FF, 0x66FF);
			tab.setPageSelectorPos(265, 248);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			//加载商品
			for (var j:int = 0; j < storeIndexes.length; j++)
			{
				var index:int = storeIndexes[j];
				objectConfig = Config.objConfigs[index];
				var storeItem:StoreItem = new StoreItem(index);
				tabs[objectConfig.storeTabIndex].addItem(storeItem, 167, 120);
				//特价区
				if (objectConfig.discount > 0)
				{
					storeItem = new StoreItem(index);
					tabs[0].addItem(storeItem, 167, 120);
				}
			}
			var textFormat:TextFormat = new TextFormat("微软雅黑", 15, 0x005500, true);
			textFormat.letterSpacing = 1.2;
			//商店标语
			for (var i:int = 0; i < 5; i++)
			{
				if (i > 0)
				{
					var topMessage:AutoScrollPane = new AutoScrollPane(360);
					var topTitleText:TextField = new TextField();
					topTitleText.selectable = false;
					topTitleText.autoSize = TextFieldAutoSize.LEFT;
					topTitleText.defaultTextFormat = textFormat;
					topTitleText.htmlText = titleInfo[i - 1];
					topMessage.addItem(topTitleText);
					topMessage.x = 0;
					topMessage.y = -35;
					tabs[i].addChild(topMessage);
				}
				if (i == 0 || i == 1 || i == 2)
				{
					//Q点按钮
					var qButton:ButtonQDian = new ButtonQDian();
					qButton.x = 420;
					qButton.y = -38;
					tabs[i].addChild(qButton);
				}
				else if (i == 3)
				{
					//光明币
					var lightCoinPane:LightCoinPane = new LightCoinPane();
					lightCoinPane.x = 420;
					lightCoinPane.y = -25;
					lightCoinPane.scaleX = 0.7;
					lightCoinPane.scaleY = lightCoinPane.scaleX;
					tabs[i].addChild(lightCoinPane);
				}
				else
				{
					//黑暗币
					var darkCoinPane:DarkCoinPane = new DarkCoinPane();
					darkCoinPane.x = 420;
					darkCoinPane.y = -25;
					darkCoinPane.scaleX = 0.7;
					darkCoinPane.scaleY = lightCoinPane.scaleX;
					tabs[i].addChild(darkCoinPane);
				}
			}
			this.setTabTitlePos(55, 118, 105); 
			this.setTabPagePos(85, 210);
			this.setCloseButtonPos(300, 489);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		
		private function onAddedToStage(event:Event):void
		{
			stage.addEventListener(Event.FULLSCREEN, onFullScreen);
			onFullScreen(null);
			var me:Player = PlayerManager.getInstance().me;
			me.packageModel.addEventListener(ModelEvent.PURCHASE_COMPLETE, onPurchaseComplete, false, 0, true);
		}

		private function onRemovedFromStage(event:Event):void
		{
			stage.removeEventListener(Event.FULLSCREEN, onFullScreen);
			var me:Player = PlayerManager.getInstance().me;
			me.packageModel.removeEventListener(ModelEvent.PURCHASE_COMPLETE, onPurchaseComplete);
		}
		
		private function onFullScreen(event:Event):void
		{
			this.x = (BFont.stageWidth - 678.85) / 2;
			this.y = (BFont.stageHeight - 561.85) / 2;
		}
		
		private function onPurchaseComplete(event:ModelEvent):void
		{
			var data:Object = event.data;
			var popMessage:PopMessage;
			if (data["err"] == 0)
			{
				var purchaseObjIndex:int = data['purchaseObjIndex'];
				//播放购买成功
				playMoveGoodsToPackage(purchaseObjIndex);
				popMessage = new PopMessage("购买成功！请在包裹中查看！", 0x00AA00);
			}
			else if (data["err"] == 100)
			{
				popMessage = new PopMessage("剩余神马币不足！", 0xFF0000);
			}
			else if (data["err"] == 104)
			{
				popMessage = new PopMessage("剩余神马元宝不足！", 0xFF0000);
			}
			else if (data["err"] == 105)
			{
				popMessage = new PopMessage("剩余光明币不足！", 0xFF0000);
			}
			else if (data["err"] == 106)
			{
				popMessage = new PopMessage("剩余黑暗币不足！", 0xFF0000);
			}
			else
			{
				popMessage = new PopMessage("购买失败！", 0xFF0000);
			}
			WidgetManager.getInstance().popOut([popMessage]);
		}
		
		//播放把物品移动到包裹
		private function playMoveGoodsToPackage(objectIndex:int):void
		{
			var objectConfig:ObjConfig = Config.objConfigs[objectIndex];
			var ObjectImage:Class = ResourceHelper.getInstance().getClassByName(objectConfig.iconImage[0]);
			var objectSprite:BasicWidget = new BasicWidget();
			objectSprite.addChild(new ObjectImage());
			objectSprite.width = 85;
			objectSprite.scaleY = objectSprite.scaleX;
			objectSprite.x = -objectSprite.width / 2;
			objectSprite.y = -objectSprite.height / 2;
			objectSprite.anchor = BasicWidget.ANCHOR_MIDDLE_MIDDLE;
			WidgetManager.getInstance().popOut([objectSprite]);
			var caseIcon:Sprite = BottomPane.getInstance().caseIcon;
			var globalPoint:Point = caseIcon.parent.localToGlobal(new Point(caseIcon.x, caseIcon.y));
			TweenLite.to(objectSprite, 1.5, {x: globalPoint.x, y: globalPoint.y, scaleX:0.5, scaleY:0.5, onComplete:objectSprite.close});
		}
		
		//设置今日的特价
		private function setSpecialOfToday(storeIndexes:Array):void
		{
			var rand:BRandom = new BRandom(ServerRequest.currentDays() + 3);
			for (var i:int = 0; i < 6; i++)
			{
				var randIndex:int = storeIndexes.length * rand.nextNumber();
				var objectConfig:ObjConfig = Config.objConfigs[storeIndexes[randIndex]];
				if (!objectConfig.notOnSale)
					objectConfig.discount = 100 - 10 * (i + 1);
			}
		}

		public static function getInstance():Store
		{
			if (!instance)
				instance = new Store();
			return instance;
		}
		
		public static function getPriceSprite(title:String, price:int, imgClassName:String, color:uint):Sprite
		{
			var res:Sprite = new Sprite();
			//价格
			var priceTitle:TextField = new TextField();
			priceTitle.selectable = false;
			priceTitle.autoSize = TextFieldAutoSize.LEFT;
			priceTitle.defaultTextFormat = new TextFormat("微软雅黑", 13, 0x000000);
			priceTitle.text = title;
			//加载价格图标和价格
			var priceContainer:Sprite = new Sprite();
			//价格图标
			var PriceImage:Class = ResourceHelper.getInstance().getClassByName(imgClassName);
			var priceImage:Sprite = new PriceImage();
			priceImage.width = 25;
			priceImage.height = 25;
			priceContainer.addChild(priceImage);
			//价格
			var priceText:TextField = new TextField();
			priceText.selectable = false;
			priceText.autoSize = TextFieldAutoSize.LEFT;
			priceText.defaultTextFormat = new TextFormat("微软雅黑", 15, color);
			priceText.text = price.toString();
			priceText.x = priceImage.x + priceImage.width + 2;
			priceText.y = priceImage.y;
			priceContainer.y = 20;
			priceContainer.addChild(priceText);
			if (priceContainer.width > priceTitle.width)
				priceTitle.x = (priceContainer.width  - priceTitle.width)/ 2;
			else
				priceContainer.x = (priceTitle.width - priceContainer.width)/ 2;
			res.addChild(priceTitle);
			res.addChild(priceContainer);
			return res;
		}
		
		//所有物品排序
		private function sortObjects(a:int, b:int):int
		{
			var objectConfigA:ObjConfig = Config.objConfigs[a];
			var objectConfigB:ObjConfig = Config.objConfigs[b];
			if (objectConfigA.index < objectConfigB.index)
			{
				return 1;
			}
			else if (objectConfigA.index > objectConfigB.index)
			{
				return -1;
			}
			return 0;
		}
	}
}